package com.dora.pattern.strategy.classic;

import com.dora.pattern.strategy.DataService;
import com.dora.pattern.strategy.Stock;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 排名服务实现类
 *
 * @author hanchen
 * @date 2024/04/16
 */
@Service
public class RankServiceImpl {

    /**
     * dataService.getSource() 提供原始的股票数据
     */
    @Resource
    private DataService dataService;

    /**
     * 前端传入榜单类型, 返回排序完的榜单
     *
     * @param rankType 榜单类型
     * @return 榜单数据
     */
    public List<Stock> getRank(String rankType) {
        // 创建上下文
        Context context = new Context();
        // 这里选择策略
        switch (rankType) {
            case "HighPrice":
                context.setStrategy(new HighPriceRank());
                break;
            case "LowPrice":
                context.setStrategy(new LowPriceRank());
                break;
            case "HighRise":
                context.setStrategy(new HighRiseRank());
                break;
            default:
                throw new IllegalArgumentException("rankType not found");
        }
        // 然后执行策略
        return context.getRank(dataService.getResource());
    }
}
